package Fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.zelonghu.testgps.AppContent;
import com.example.zelonghu.testgps.R;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import Entities.CatAdoptList;
import Entities.ClothesDonationList;
import Entities.LibraryRegisterList;
import SQLUtils.sqllite;

/**
 * @author zelonghu QQ:975169216
 * @name testGPS2
 * @class name：Fragment
 * @class describe
 * @time 2019/6/30 9:47 PM
 * @change
 * @chang time
 * @class 我的任务页面，用于显示正在进行的事件，可以点击退出，也可以查看进度
 */
public class TaskFragment extends Fragment {
    ListView listView;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //sqllite.droptable(sqllite.getDB(getContext()), AppContent.library_register_list_tablename);
        View view = inflater.inflate(R.layout.testgps2_fragment_task,container,false);
        //初始化listview
        listView = (ListView)view.findViewById(R.id.taskfragment_listview);
        listView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return 0;
            }

            @Override
            public Object getItem(int i) {
                return null;
            }

            @Override
            public long getItemId(int i) {
                return 0;
            }

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                return null;
            }
        });
        //todo 插入所有的正在进行的捐书活动
        List<LibraryRegisterList> ll = SQLUtils.sqllite.query_Library_Register_List_Desc(sqllite.getDB(getContext()));
        for(int i = 0;i<ll.size();i++){

            LibraryRegisterList l = ll.get(i);

            View v = View.inflate(getActivity(), R.layout.testgps2_fragment_task_listitem_library, null);
            TextView name = v.findViewById(R.id.fragment_task_listitem_itemname);
            name.setText("捐书" + l.getLBookNumber() +"");

            v.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {

                    final  String name= ((TextView)view.findViewById(R.id.fragment_task_listitem_itemname)).
                            getText().toString();

                    final AlertDialog alert =  new AlertDialog.Builder(getContext())
                            .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Log.i("delete",name.substring(2));
                                    sqllite.deleteLname(sqllite.getDB(getContext()),name.substring(2));
                                    dialogInterface.dismiss();

                                }
                            }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    dialogInterface.dismiss();
                                }
                            }).setTitle("删除任务").create();

                    alert.show();
                    return true;
                }
            });

            TextView status = v.findViewById(R.id.fragment_task_listitem_itemstate);
            status.setText(l.getLState());
            listView.addFooterView(v);
        }
        //todo 插入所有的流浪猫活动
        List<CatAdoptList> cal = sqllite.query_Cat_adopt_List_Desc(sqllite.getDB(getContext()));
        for(CatAdoptList c:cal){
            View v = View.inflate(getActivity(), R.layout.testgps2_fragment_task_listitem_library, null);
            TextView name = v.findViewById(R.id.fragment_task_listitem_itemname);
            name.setText("收养" + c.getCattype() +"流浪猫");
            Log.i("delete",c.toString());

            v.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {

                    final  String name= ((TextView)view.findViewById(R.id.fragment_task_listitem_itemname)).
                            getText().toString();

                    final AlertDialog alert =  new AlertDialog.Builder(getContext())
                            .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    sqllite.delete_cat_adopt_type(sqllite.getDB(getContext()),name.substring(2,name.length()-3));
                                    dialogInterface.dismiss();

                                }
                            }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    dialogInterface.dismiss();
                                }
                            }).setTitle("删除任务").create();

                    alert.show();
                    return true;
                }
            });
            TextView status = v.findViewById(R.id.fragment_task_listitem_itemstate);
            status.setText(c.getState());
            listView.addFooterView(v);
        }
        //todo 插入捐衣活动
        List<ClothesDonationList> cdl = sqllite.query_Clothes_donation_List_Desc(sqllite.getDB(getContext()));
        for(ClothesDonationList cd:cdl){
            View v = View.inflate(getActivity(), R.layout.testgps2_fragment_task_listitem_library, null);
            TextView name = v.findViewById(R.id.fragment_task_listitem_itemname);
            name.setText("捐献" +cd.getCDdonate_number()  +"衣服");

            v.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {

                    final  String name= ((TextView)view.findViewById(R.id.fragment_task_listitem_itemname)).
                            getText().toString();

                    final AlertDialog alert =  new AlertDialog.Builder(getContext())
                            .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    sqllite.delete_clothes_donation_number(sqllite.getDB(getContext()),name.substring(2,name.length()-2));
                                    dialogInterface.dismiss();

                                }
                            }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    dialogInterface.dismiss();
                                }
                            }).setTitle("删除任务").create();

                    alert.show();
                    return true;
                }
            });
            TextView status = v.findViewById(R.id.fragment_task_listitem_itemstate);
            status.setText(cd.getCDState());
            listView.addFooterView(v);
        }

//        View v = View.inflate(getActivity(), R.layout.testgps2_fragment_task_listitem_library, null);
//         //View layout = inflater.inflate(R.layout.testgps2_fragment_task_listitem_library, container, false);
//        listView.addFooterView(v);
//         v = View.inflate(getActivity(), R.layout.testgps2_fragment_task_listitem_library, null);
//        //View layout = inflater.inflate(R.layout.testgps2_fragment_task_listitem_library, container, false);
//        listView.addFooterView(v);
//        v = View.inflate(getActivity(), R.layout.testgps2_fragment_task_listitem_library, null);
//        //View layout = inflater.inflate(R.layout.testgps2_fragment_task_listitem_library, container, false);
//        listView.addFooterView(v);
        //todo 从数据库中寻找到能添加的东西
        //todo 绘制view



        //todo 绘制完成后添加到listview
        return view;
    }


}
